package com.yanqu.road.server.manager.chefduel;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.chefduel.ChefDuelMatchDish;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.server.pb.chefduel.CrossChefDuelPb;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossChefDuel {

    private Logger logger = LogManager.getLogger(CrossChefDuel.class.getName());

    private ActivityInfo activityInfo;

    public int getActivityId(){
        return activityInfo.getActivityId();
    }

    public ActivityInfo getActivityInfo(){
        return activityInfo;
    }

    public boolean activityInWindUpTime() {
        if(null == activityInfo){
            return false;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        return nowTime >= activityInfo.getEndTime() + 5 * 60 && nowTime < activityInfo.getEndShowTime();
    }

    //活动结束结算下发晋级人员
    public void activityEndWindUp(){
        if(activityInWindUpTime()){

//            logger.error("清除阶段1结束数据");
            //同步给本服
            Map<Long, List<ChefDuelProto.CrossChefDuelWindUpCookerTempMsg>> goodBoyServerSyncMap = new ConcurrentHashMap<>();
            int maxRank = CrossChefDuelActivityMgr.getCook1ToCook2Rank(getActivityId());
            //厨神对决一阶段改成new rank
            CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityNewRankListModel(getActivityId(), eGamePlayerEventType.ChefDuel1StageScoreRank.getValue());
            if(rankListModel == null){
                //启动的时候可能还没全部启动完定时器已经开始跑了，排行榜还没加载完。。。。
                return;
            }
            LinkedList<CrossUserRank> rankList = rankListModel.getRankList();
            int rank = 1;
            for(CrossUserRank userRank : rankList){
                if(rank <= maxRank){
                    //logger.debug("厨神对决阶段1晋级阶段2下发数据，配置文件晋级的排名要求"+rank+",晋级的玩家id:"+userRank.getUserId()+",玩家排名"+rank+",userRank排名:"+userRank.getRank());
                    if(!goodBoyServerSyncMap.containsKey(userRank.getUserBaseInfo().getServerId())){
                        goodBoyServerSyncMap.put(userRank.getUserBaseInfo().getServerId(),new ArrayList<>());
                    }
                    ChefDuelProto.CrossChefDuelWindUpCookerTempMsg.Builder builder = ChefDuelProto.CrossChefDuelWindUpCookerTempMsg.newBuilder();
                    builder.setRank(userRank.getRank());
                    builder.setScore(userRank.getValue().longValue());
                    builder.setUserId(userRank.getUserId());
                    goodBoyServerSyncMap.get(userRank.getUserBaseInfo().getServerId()).add(builder.build());
                }
                rank++;
            }
//            int activityId = CrossChefDuelMgr.windUpChefDuel1User(getActivityId(),goodBoyMap);
//            logger.info("厨神对决一阶段进入二阶段排行榜同步到游戏服,同步大小{}",rank);

            windUpChefDuel1UserSyncServer(goodBoyServerSyncMap);
        }
    }

    public void windUpChefDuel1UserSyncServer(Map<Long,  List<ChefDuelProto.CrossChefDuelWindUpCookerTempMsg>> goodBoyServerSyncMap){
        for(Long serverId : goodBoyServerSyncMap.keySet()){
            ChefDuelProto.CrossChefDuelWindUpMsg.Builder respMsg =
                    CrossChefDuelPb.parseCrossChefDuelWindUpMsg(activityInfo.getActivityId(), goodBoyServerSyncMap.get(serverId));
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_CHEF_DUEL_TO_CHEF_DUEL2, respMsg);
            MessageHelper.sendPacket(serverId, 0, pbMsg);
        }
    }

    public void windUpChefDuel1UserSyncServer(int cookBoy2AcId ,Map<Long,List<Long>> goodBoyServerSyncMap){
//        if(cookBoy2AcId == 0){
//            return;
//        }
//        for(Long serverId : goodBoyServerSyncMap.keySet()){
//            ChefDuelProto.CrossChefDuelWindUpMsg.Builder respMsg =
//                    CrossChefDuelPb.parseCrossChefDuelWindUpMsg(activityInfo.getActivityId(),goodBoyServerSyncMap.get(serverId));
//            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_CHEF_DUEL_TO_CHEF_DUEL2, respMsg);
//            MessageHelper.sendPacket(serverId, 0, pbMsg);
//        }
    }


    private int getUserFoodCount(long userId,Map<Integer, ChefDuelMatchDish> matchFoodMap){
        int count = 0;
        for(ChefDuelMatchDish tempFood : matchFoodMap.values()){
            if(tempFood.getUserId() == userId)count++;
        }
        return count;
    }



    public void timerTask(){

    }

    public void syncLoginData(){

    }

    public CrossChefDuel(ActivityInfo acInfo){
        activityInfo = acInfo;
    }

    public boolean reload() {
        return true;
    }

    public boolean save() {
        return true;
    }

    public boolean init() {
        return reload();
    }

    public boolean stop() {
        return false;
    }

    /**
     * 预留接口
     */
    public void repair() {

    }

    public void setActivityInfo(ActivityInfo activityInfo){
        this.activityInfo = activityInfo;
    }
}
